package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 11:10
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    @Override
    public PageInfo<ProductCategoryTreeNodeVO> categoryTree(String pageNum, String pageSize) {
        if(!StringUtils.isEmpty(pageNum)&&!StringUtils.isEmpty(pageSize)){
            PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        }
        //父级查询
        int lev = 0;
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryTreeNodeRe(lev,0L);
        return new PageInfo<ProductCategoryTreeNodeVO>(productCategoryTreeNodeVOList);

    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        //查询子分类
        int lev = 0;
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryTreeNodeRe(lev,0L);
        return productCategoryTreeNodeVOList;
    }

    @Override
    public void addProductCategory(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        /*ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);*/
        return productCategory;
    }

    /*@Override
    public void update(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }*/

    @Override
    public void update(ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public void delete(Long id) {
        productCategoryMapper.deleteByPrimaryKey(id);
    }

    private List<ProductCategoryTreeNodeVO> productCategoryTreeNodeRe(int lev,Long pid) {
        //子级查询
        lev ++;
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",pid);
        example.setOrderByClause("sort asc");
        List<ProductCategory> productCategoriesList = productCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(productCategoriesList)){
            lev --;
            return null;
        }
        //封装到VO
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoriesList) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
            /*if(Long.parseLong("0") == (productCategoryTreeNodeVO.getPid())){
                lev = 1;
            }*/
            productCategoryTreeNodeVO.setLev(lev);
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOReturnList = productCategoryTreeNodeRe(lev,productCategoryTreeNodeVO.getId());//迭代
            productCategoryTreeNodeVO.setChildren(productCategoryTreeNodeVOReturnList);
            productCategoryTreeNodeVOList.add(productCategoryTreeNodeVO);
        }
        lev --;
        return productCategoryTreeNodeVOList;




    }
}
